package com.huahai.security.core.validate.code.mobile.sms;

import com.huahai.security.core.properties.SecurityProperties;
import com.huahai.security.core.validate.code.ValidateCode;
import com.huahai.security.core.validate.code.ValidateCodeGenerator;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: Jun
 * @Date: 2020/7/20
 *
 * 短信验证码生成器
 */
@Component("smsValidateCodeGenerator")
public class SmsCodeGenerator implements ValidateCodeGenerator {

    private SecurityProperties securityProperties;

    /**
     * 生成方法
     * @param request
     * @return
     */
    @Override
    public ValidateCode generate(HttpServletRequest request) {
        // 生成随机数字字符串 ,长度可以自由配置
        String code = RandomStringUtils.randomNumeric(securityProperties.getCode().getSms().getLength());
        return new ValidateCode(code, securityProperties.getCode().getSms().getExpireIn());
    }

    @Autowired
    public void setSecurityProperties(SecurityProperties securityProperties) {
        this.securityProperties = securityProperties;
    }
}